package cn.zlp.org.service.impl;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.service.impl.BaseServiceImpl;
import cn.zlp.basic.util.PageList;
import cn.zlp.org.domain.Employee;
import cn.zlp.org.domain.Shop;
import cn.zlp.org.dto.ShopPetDto;
import cn.zlp.org.mapper.EmployeeMapper;
import cn.zlp.org.mapper.ShopMapper;
import cn.zlp.org.service.IShopService;
import cn.zlp.pet.domain.Pet;
import cn.zlp.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺Service类,先继承公共的Service类,再实现接口
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired(required = false)
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeServiceImpl employeeService;

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public void register(Shop shop) {
        // 基本信息审核
        /*// 获取入驻店铺地址
        String address = shop.getAddress();
        // 根据地址查询店铺信息
        Shop shop1 = shopMapper.findByAddress(address);*/
        Employee employee1 = employeeMapper.findByAccount(shop.getAdmin());
        if (employee1 == null){ // 如果店铺为null,说明还没有入驻过,可以入驻
            // 基础审核通过,将状态赋值为待激活
            shop.setState(2);
            // 获取管理员信息
            Employee employee = shop.getAdmin();
            // 设置管理员状态
            employee.setState(shop.getState());
            // 设置管理员登录信息为店铺管理员
            Logininfo logininfo = new Logininfo();
            logininfo.setType(0);
            employee.setLogininfo(logininfo);
            // 添加管理员
            employeeService.add(employee);
            // 获取自增长主键,赋给店铺中管理员id
            shop.setAdmin_id(employee.getId());
            // 添加店铺
            shopMapper.save(shop);
            // 设置管理员的店铺id
            employee.setShop_id(shop.getId());
            // 修改管理永远,再数据库中存储
            employeeMapper.update(employee);
        } else { // 地址重复,不满住
            throw new BusinessException("重复入驻,请直接登录,忘记密码请找回!");
        }
    }

    /**
     * 分页查询店铺宠物信息
     * @param shopPetDto
     * @return
     */
    @Override
    public PageList<Pet> findShopPetPage(ShopPetDto shopPetDto) {
        Long totals = shopMapper.getPetTotals(shopPetDto);
        if (totals == null || totals < 1){
            return new PageList<>();
        }
        List<Pet> list = shopMapper.loadPetByQuery(shopPetDto);
        return new PageList<>(totals,list);
    }

    @Override
    public void update(Shop shop) {
        // 因为传过来的管理员参数在对象中,所以我们要取出来赋值给管理员id
        shop.setAdmin_id(shop.getAdmin().getId());
        // 修改赋值后的管理员
        shopMapper.update(shop);
    }

    @Override
    public void del(Long id) {
        // 传过来的是id,我们要获取管理员id,然后将店铺管理员和登录信息一并删除了
        Shop shop = shopMapper.loadById(id);
        // employeeService实现类中写了关联删除,所以直接调用,比较懒
        employeeService.del(shop.getAdmin_id());
        // 删除店铺
        shopMapper.delete(id);
    }
}
